// src/main/db/index.js
const Sequelize = require('sequelize')
const requireDirectory = require('require-directory')

const dbConfig = {
  username: 'root',
  password: '123456',
  host: 'localhost',
  database: 'test',
  dialect: 'mysql',
  define: {
    // 数据库字段下划线命名，匹配模型驼峰式命名
    underscored: true,
    // 禁用修改表名
    freezeTableName: true
  },
  sync: false,
  timezone: '+08:00',
  // 返回的时间格式配置
  dialectOptions: {
    dateStrings: true,
    typeCast: true
  }
}

// console.log(config.db);
const sequelize = new Sequelize(dbConfig);

// 加载模型
requireDirectory(module, '../model', {
  visit: (obj) => {
    if (obj.modelName) {
      sequelize.define(obj.modelName, obj.attributes, obj.options);
      sequelize.sync({
        force: false
      });
    }
  }
});

module.exports = sequelize
